package com.tianma315.core.merchant.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.core.agent.dao.AgentDao;
import com.tianma315.core.attr.dao.AttrDao;
import com.tianma315.core.common.base.CoreServiceImpl;
import com.tianma315.core.common.utils.MD5Utils;
import com.tianma315.core.constant.StateEnum;
import com.tianma315.core.merchant.dao.MerchantDao;
import com.tianma315.core.merchant.domain.MerchantDO;
import com.tianma315.core.merchant.service.MerchantService;
import com.tianma315.core.merchant.vo.MerchantVO;
import com.tianma315.core.oss.service.FileService;
import com.tianma315.core.sys.dao.UserRoleDao;
import com.tianma315.core.sys.domain.UserDO;
import com.tianma315.core.sys.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;

/**
 * 
 * <pre>
 * 商户表
 * </pre>
 * <small> 2019-08-01 09:20:13 | gwl</small>
 */
@Service
public class MerchantServiceImpl extends CoreServiceImpl<MerchantDao, MerchantDO> implements MerchantService {
    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private AgentDao agentDao;
    @Autowired
    private MerchantDao merchantDao;


    @Autowired
    private AttrDao attrDao;


    @Override
    @Transactional
    public Boolean insertMerchant(MerchantVO merchantvO) throws IOException {
        MerchantDO merchantDO = new MerchantDO();
        String url = fileService.upload(merchantvO.getFile().getBytes(),merchantvO.getFile().getOriginalFilename());
        BeanUtils.copyProperties(merchantvO,merchantDO);
        merchantDO.setLogo(url);
        merchantDO.setCreateDate(Calendar.getInstance().getTime());
        merchantDO.setState(StateEnum.not_del.getCode());
        Boolean result = insert(merchantDO);

        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(merchantvO,userDO);
        userDO.setMobile(merchantDO.getTel());
        Long roleId = Long.valueOf(57);
        List<Long> roleIds = Arrays.asList(roleId);
        userDO.setroleIds(roleIds);
        userDO.setMerchantId(merchantDO.getMerchantId());
        userDO.setName(merchantDO.getMerchantName());
        userDO.setPassword(MD5Utils.encrypt(userDO.getUsername(), userDO.getPassword()));
        userDO.setStatus(1);//1为可用
        userService.insert(userDO);

//        UserRoleDO userRoleDO = new UserRoleDO();
//        Long roleId = Long.valueOf(57);
//        userRoleDO.setRoleId(roleId);
//        userRoleDO.setUserId(userDO.getId());
//        userRoleDao.insert(userRoleDO);

        return result;
    }

    @Override
    public Page<MerchantDO> getMerchantDOPage(Integer pageNumber, Integer pageSize, MerchantDO merchantDTO) {
        Page<MerchantDO> page = new Page<>(pageNumber,pageSize);
        EntityWrapper<MerchantDO> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("state",StateEnum.not_del.getCode());
        page = selectPage(page, entityWrapper);
        int total = selectCount(entityWrapper);
        page.setTotal(total);
        return page;
    }

    @Override
    public Boolean deleteMerchantService(Long id) {
        MerchantDO merchantDO = new MerchantDO();
        merchantDO.setState(StateEnum.is_del.getCode());
        merchantDO.setMerchantId(id);
        Boolean result = updateById(merchantDO);
        return result;
    }

    @Override
    public boolean removeInfo(Long merchantId) {
        //清除经销商和经销商地址
        int agentNumber = agentDao.deleteByMerchantId(merchantId);
        //清楚属性和属性值
        int deleteByMerchantId = attrDao.deleteByMerchantId(merchantId);

        return false;
    }

    @Override
    public MerchantDO getByUserId(long user_id) {
        return merchantDao.selectByUserId(user_id);
    }

    @Override
    public MerchantDO getById(long merchant_id) {
        return merchantDao.selectById(merchant_id);
    }

}
